from setting import *


class Lifebar(pygame.sprite.Sprite):
        """
        显示一条和BOSS宽度一样的血条
            随着BOSS生命的减少，按比例减少，当BOSS毁灭时，也跟着殉葬 """
        def __init__(self, boss):
            self.groups = allgroup
            self.boss = boss
            self._layer = self.boss._layer
            pygame.sprite.Sprite.__init__(self, self.groups)
            self.oldpercent = 0
            self.paint()
            
        def paint(self):
            self.image = pygame.Surface((self.boss.rect.width,12))
            self.image.set_colorkey((0,0,0)) # 黑色透明
            #设置边框
            pygame.draw.rect(self.image, (0,255,0), (0,0,self.boss.rect.width,10),1)
            pygame.draw.rect(self.image,(0,255,0),(0,0,self.boss.rect.width,10),10)
            self.rect = self.image.get_rect()
 
        def update(self):
            self.percent = self.boss.HP / self.boss.HPFULL * 1.0
            #x血量变化后，重绘
            if self.percent != self.oldpercent:
                self.paint() # bOSS变形后，血条尺寸会变的，需要重新计算尺寸
                #设置底色黑色，加上paint的colorkey，黑色就变透明色了。
                pygame.draw.rect(self.image, (0,0,0), (1,1,self.boss.rect.width-2,10)) 
                #绘制
                if self.percent >0.25:
                    pygame.draw.rect(self.image, (0,255,0), (1,1,int(self.boss.rect.width * self.percent),10)) 
                elif self.percent <= 0.25:
                    pygame.draw.rect(self.image, (255,0,0), (1,1,int(self.boss.rect.width * self.percent),10)) 
            self.oldpercent = self.percent
            self.rect.centerx = self.boss.rect.centerx
            self.rect.centery = self.boss.rect.centery - self.boss.rect.height /2 - 10
            if self.boss.HP < 1:   #check if boss is still alive
                self.kill()